package com.hq.controller;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.hq.bean.Student;
import com.hq.service.StuService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class StuController {
    @Resource
    StuService stuService;

    @RequestMapping("/login.do")
    public String login (Student student, HttpSession session){
        Student login = stuService.login(student.getName(), student.getPassword());
        if(login!=null){
            session.setAttribute("user",login);
            if(login.getRole()==0){
                return "redirect:/listguanliyuan.do";
            }else if(login.getRole()==1){
                return "redirect:/liststu.do";
            }
        }else{
            return "redirect:index.jsp";
        }
        return null;
    }

    @RequestMapping("/liststu.do")
    public String liststu(Model model,HttpSession session){
        Student user = (Student)session.getAttribute("user");
        List liststu=stuService.liststu(user.getId());
        model.addAttribute("liststu",liststu);
        return "liststu";
    }

    @RequestMapping("/listguanliyuan.do")
    public String listguanliyuan(Model model, @RequestParam(defaultValue = "1")int pageNum,String floorname,String name){
        Page<Object> page = PageHelper.startPage(pageNum, 3);
        List listguanliyuan=stuService.listguanliyuan(floorname,
                name);
        model.addAttribute("listguanliyuan",listguanliyuan);
        model.addAttribute("page",page);
        model.addAttribute("floorname",floorname);
        model.addAttribute("name",name);
        return "listguanliyuan";
    }

    @RequestMapping("/ckxq.do")
    public String ckxq(Integer id){
        return "upd";
    }

    @RequestMapping("/chakan.do")
    @ResponseBody
    public Object chakan(Integer id,HttpSession session){
        List list = stuService.ckxq(id);
        List stu=stuService.findstu(id);
        session.setAttribute("stu",stu);
        return list;
    }
}
